package org.example.mapper;

import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.example.dto.ProductSpecDTO;
import org.example.entity.Category;
import org.example.entity.Product;
import org.example.entity.Shanpin;
import org.example.result.Pages;
import org.example.vo.CategoryVO;
import org.example.vo.ProductSpecVO;

import java.util.List;

@Mapper
public interface Shanpinmapper {

    /**
     * 商品分类分页查询
     * @param pages
     * @return
     */
    Page<Shanpin> pageQuery(Pages pages);

    /**
     * 新增商品
     *
     * @param produc
     * @param
     */
    void xz(Product produc);

    @Insert("INSERT INTO product (name,price,stock,weight,location,template,shanpin_id)\n" +
            "VALUES\n" +
            "(#{name},#{price},#{stock},#{weight},#{location},#{template},#{id})")
    void xzproduc(Product product);

    @Insert("INSERT INTO product_image (image,shanpin_id)\n" +
            "VALUES\n" +
            "(#{newname},#{id})")
    void xzimgae(int id, String newname);

    void xzcategory(Category category);


    void xzspec(ProductSpecDTO productSpecDTO);

    @Insert("INSERT INTO product_options (name,shanpin_id,spec_id)\n" +
            "VALUES\n" +
            "(#{p},#{id},#{id1})")
    void xzoptions(int id, String p, int id1);

    /**
     * 修改查询
     * @param id
     * @return
     */
    @Select("select * from product where shanpin_id=#{id}")
    Product cxproduct(int id);

    @Select("select shanpinfele_name from shanpin where id=#{id}")
    String cxfele(int id);

    @Select("select id,name from product_category where shanpin_id=#{id}")
    CategoryVO cxcategory(int id);

    @Select("select id,name from product_spec where category_id=#{id} and shanpin_id=#{id1}")
    List<ProductSpecVO> cxspec(String id, int id1);

    @Select("select name from product_options where spec_id=#{id} ")
    List<String> cxoptions(int id);

    @Select("select image from product_image where shanpin_id=#{id} ")
    List<String> cximage(int id);

    @Update("UPDATE product_image\n" +
            "SET shanpin_id=#{id}\n" +
            "WHERE shanpin_id=#{i}")
    void xgimage(int i,int id);

    /**
     * 修改删除
     * @param id
     */
    @Delete("delete from shanpin where id=#{id}")
    void xgscshanpin(int id);
    @Delete("delete from product where shanpin_id=#{id}")
    void xgscproduct(int id);

    @Delete("delete from product_category where shanpin_id=#{id}")
    void xgsccategory(int id);

    @Delete("delete from product_spec where shanpin_id=#{id}")
    void xgscspec(int id);
    @Delete("delete from product_options where shanpin_id=#{id}")
    void xgscoptions(int id);

    @Delete("delete from product_image where image=#{url}")
    void xgscimage(String url);


    /**
     * 修改状态
     * @param product
     */
    @Update("UPDATE shanpin\n" +
            "SET status=#{status}\n" +
            "WHERE id=#{id}")
    void xgzt(Product product);

    /**
     * 单删除
     * @param id
     */
    @Delete("delete from product_image where shanpin_id=#{id}")
    void xgdanscimage(int id);

    /**
     * 查询商品
     * @param id
     * @return
     */
    @Select("select * from shanpin where id=#{id} ")
    Shanpin cxid(int id);

    /**
     * 批量删除
     *
     * @param name
     * @param status
     * @param felename
     * @param username
     * @return
     */
    List<Integer> plsccx(String name, String status, String felename, String username);

    /**
     * 批量查询
     *
     * @return
     */
    @Select("select  * from product")
    List<Product> productList();

    /**
     * 照片
     * @param id
     * @return
     */
    @Select("select image from product_image where shanpin_id=#{id}  ")
    String[] zhaopian(int id);

    /**
     * 条件查询
     * @param
     * @return
     */
    @Select("select * from shanpin where id=#{id} and username=#{username}")
    Shanpin cxtjid(int id,String username);

    /**
     * 相关查询
     * @param
     * @param shanpinfeleName
     * @return
     */
    @Select("select * from shanpin where id=#{id} and shanpinfele_name=#{shanpinfeleName}")
    Shanpin cxtxgjid(int id, String shanpinfeleName);

    /**
     * 商品搜索查询
     * @param shanpinId
     * @return
     */
    @Select("SELECT * FROM shanpin WHERE id = #{shanpinId} " +
            "AND (name LIKE CONCAT('%', #{souso}, '%') OR shanpinfele_name LIKE CONCAT('%', #{souso}, '%'))")
    Shanpin cxsousoid(int shanpinId, String souso);

    /**
     * 修改数
     * @param shanpinid
     */
    @Update("UPDATE shanpin\n" +
            "SET gomaishu=1+gomaishu,shu=shu-1\n" +
            "WHERE id=#{shanpinid}")
    void xgshu(int shanpinid);

    /**
     * 启数目
     * @param username
     * @return
     */
    @Select("select count(*) from shanpin where username=#{username} and status=1")
    int shucx(String username);

    /**
     * 启数目
     * @param username
     * @return
     */
    @Select("select count(*) from shanpin where username=#{username} and status=0")
    int shutcx(String username);

    /**
     * 修改数
     * @param shanpinid
     */
    @Update("UPDATE product\n" +
            "SET stock=stock-1\n" +
            "WHERE shanpin_id=#{shanpinid}")
    void xgshuproduct(int shanpinid);
}
